import Layout from '@/layout/index.vue';

/**
 * 通过递归过滤异步路由表
 * @param routes asyncRoutes
 * @param roles
 */
export function filterAsyncRoutes(routes, roles) {
    const res = []
    routes.forEach(route => {
        const tmp = { ...route }
        //路由表配置里面的roles里面含有该用户的路由身份则添加路由
        if (hasPermission(roles, tmp)) {
            if (tmp.children) {
                tmp.children = filterAsyncRoutes(tmp.children, roles)
            }
            res.push(tmp)
        }
    })
    return res
}

/**
 * 使用 meta.role 来确定当前用户是否具有权限
 * @param roles
 * @param route
 */
export function hasPermission(roles, route) {
    if (route.meta && route.meta.roles) {
        return roles.some(role => route.meta.roles.includes(role))
    } else {
        return false
    }
}

/**
 * @description 使用递归，过滤需要缓存的路由
 * @param {Array} _route 所有路由表
 * @param {Array} _cache 缓存的路由表
 * @return void
 * */

export function filterKeepAlive(routers) {
    let cacheRouter: any[] = [];
    let deep = (routers) => {
        routers.forEach(item => {
            if (item.meta?.keepAlive && item.name) {
                cacheRouter.push(item.name)
            }
            if (item.children && item.children.length) {
                deep(item.children)
            }
        })
    }
    deep(routers)
    return cacheRouter
}

let modules = import.meta.glob('../views/**/*.vue')
//路由全局处理
export function routersHandle(rou, state = true) {
    if (!rou?.length) return
    let arr = rou.map((item, indx) => {
        let obj = {}
        let param = {
            hidden: false,
            activeMenu: ''
        }
        if (item.param) {
            param = JSON.parse(item.param)
        }
        if (state) {
            obj = {
                hidden: param?.hidden ? true : false,
                name: item.path,
                // name: 'sadada' + indx,
                path: item.path,
                meta: {
                    icon: item.icon,
                    title: item.name,
                    activeMenu: param?.activeMenu
                },
                component: item.children?.length ? Layout : modules[`../views${item.path}.vue`],
            }
        } else {
            obj = {
                hidden: param?.hidden ? true : false,
                name: item.path,
                // name: 'sadada' + indx,
                path: item.path,
                meta: {
                    icon: item.icon,
                    title: item.name,
                    activeMenu: param?.activeMenu
                },
                component: modules[`../views${item.path}.vue`],
            }
        }
        if (item.children?.length) {
            obj.alwaysShow = true
            obj.children = routersHandle(item.children, false)
        }
        return obj
    })
    return arr
}
